import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/*
 * @lc app=leetcode.cn id=1109 lang=java
 *
 * [1109] 航班预订统计
 */

// @lc code=start
class Solution {
    public int[] corpFlightBookings(int[][] bookings, int n) {

        Map<Integer, Integer> map = new HashMap<>();
        int[] h2 = new int[n];
        int num = 0;
        for (int i = 0; i < bookings.length; i++) {
            int[] d = bookings[i];
            if (map.containsKey(d[0])) {
                map.put(d[0], map.get(d[0]) + d[2]);
            } else {
                map.put(d[0], d[2]);
                h2[num] = d[0];
                num++;
            }

            if (d[1] - d[0] > 1) {
                int l = d[0];
                for (int j = 1; l < d[1] - 1; j++) {
                    l = d[0] + j;
                    if (map.containsKey(l)) {
                        map.put(l, map.get(l) + d[2]);
                    } else {
                        map.put(l, d[2]);
                        h2[num] = l;
                        num++;
                    }
                }
            }

            if (map.containsKey(d[1])) {
                if (d[0] != d[1]) {
                    map.put(d[1], map.get(d[1]) + d[2]);
                }

            } else {
                map.put(d[1], d[2]);
                h2[num] = d[1];
                num++;
            }
        }
        System.out.println("map.size: " + map.size() + " n:" + n);
        if (map.size() < n) {
            if (!map.containsKey(n)) {
                for (int k = n; k > 1; k--) {
                    if (!map.containsKey(k)) {
                        map.put(k, 0);
                        h2[num] = k;
                        num++;
                    } else {
                        break;
                    }
                }
            }
            if (!map.containsKey(1)) {
                for (int y = 1; y < n; y++) {
                    if (!map.containsKey(y)) {
                        map.put(y, 0);
                        h2[num] = y;
                        num++;
                    } else {
                        break;
                    }
                }
            }
        }
        System.out.println("h2: " + Arrays.toString(h2));
        Arrays.sort(h2);
        int[] s = new int[h2.length];

        for (int i = 0; i < h2.length; i++) {
            s[i] = map.containsKey(h2[i]) ? map.get(h2[i]) : 0;
        }
        System.out.println("result: " + Arrays.toString(s));
        return s;
    }
}
// @lc code=end

//
// 1 2 3